﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace PRO_DOCS.DocumentManagementSystem.ADMIN.WIN.Configurations.Security
{
    public partial class ucDeniedLoginUsers : ucBaseSecurity
    {

        public ucDeniedLoginUsers()
        {
            InitializeComponent();

            try
            {
                PRO_DOCSDatabaseDataContext.proc_LOGIN_USERSLoadAll().ToList().ForEach(new Action<PRO_DOCS.DAL.Database.proc_LOGIN_USERSLoadAllResult>(delegate(PRO_DOCS.DAL.Database.proc_LOGIN_USERSLoadAllResult _loginUser)
                {
                    CheckedListBoxItem listViewItem = new CheckedListBoxItem();
                    listViewItem.Value = _loginUser.LOGIN_USER_ID.ToString();
                    listViewItem.Description = _loginUser.EMPLOYEE_FULL_NAME;
                    lstLoginUsers.Items.Add(listViewItem);
                }));
            }
            catch { }
        }

        public void SAVE_DENIED_LOGIN_USERS(long archive_department_id, Guid _archive_department_folderstructure_id, int archive_module_id)
        {
            switch (CURRENT_SELECTED_SECURITY_OPTION)
            {
                case SecurityOptions.N_A:
                    break;
                #region DepartmentsSecurity
                case SecurityOptions.DepartmentsSecurity:
                    {
                        //1. Delete old data
                        PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENT_DENIED_LOGIN_USERSDeleteByARCHIVE_DEPARTMENT_ID(archive_department_id);
                        //2. save the new data
                        for (int i = 0; i < lstLoginUsers.ItemCount; i++)
                        {
                            if (lstLoginUsers.Items[i].CheckState.Equals(CheckState.Checked))
                            {
                                PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENT_DENIED_LOGIN_USERSInsert(archive_department_id, Convert.ToInt64(lstLoginUsers.Items[i].Value.ToString()));
                            }
                        }
                    }
                    break;
                #endregion
                case SecurityOptions.FolderStructureSecurity:
                    {
                        //1. Delete old data
                        PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_DENIED_LOGIN_USERSDeleteBy_FOLDERS_STRUCTURE_ID(_archive_department_folderstructure_id);
                        //2. save the new data
                        for (int i = 0; i < lstLoginUsers.ItemCount; i++)
                        {
                            if (lstLoginUsers.Items[i].CheckState.Equals(CheckState.Checked))
                            {
                                PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_DENIED_LOGIN_USERSInsert(_archive_department_folderstructure_id, Convert.ToInt64(lstLoginUsers.Items[i].Value.ToString()));
                            }
                        }
                    }
                    break;
                case SecurityOptions.ModulesSecurity:
                    {
                        //1. Delete old data
                        PRO_DOCSDatabaseDataContext.proc_APPLICATION_MODULE_DENIED_USERSDeleteByAPPLICATION_MODULE_ID(archive_module_id);
                        //2. save the new data
                        for (int i = 0; i < lstLoginUsers.ItemCount; i++)
                        {
                            if (lstLoginUsers.Items[i].CheckState.Equals(CheckState.Checked))
                            {
                                PRO_DOCSDatabaseDataContext.proc_APPLICATION_MODULES_DENIED_USERSInsert(archive_module_id, Convert.ToInt64(lstLoginUsers.Items[i].Value.ToString()));
                            }
                        }
                    }
                    break;
                default:
                    break;
            }
        }

        public void LOAD_DENIED_LOGIN_USERS(SecurityOptions _securityOptions, long archive_department_id, Guid _archive_department_folderstructure_id, int archive_module_id)
        {
            lstLoginUsers.UnCheckAll();
            CURRENT_SELECTED_SECURITY_OPTION = _securityOptions;
            switch (CURRENT_SELECTED_SECURITY_OPTION)
            {
                case SecurityOptions.N_A:
                    break;
                #region DepartmentsSecurity
                case SecurityOptions.DepartmentsSecurity:
                    {
                        PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENT_DENIED_LOGIN_USERSLoadAllByARCHIVE_DEPARTMENT_ID(archive_department_id).ToList().ForEach(new Action<PRO_DOCS.DAL.Database.proc_ARCHIVE_DEPARTMENT_DENIED_LOGIN_USERSLoadAllByARCHIVE_DEPARTMENT_IDResult>(delegate(PRO_DOCS.DAL.Database.proc_ARCHIVE_DEPARTMENT_DENIED_LOGIN_USERSLoadAllByARCHIVE_DEPARTMENT_IDResult _archive_department_denied_user)
                        {
                            for (int i = 0; i < lstLoginUsers.ItemCount; i++)
                            {
                                if (lstLoginUsers.Items[i].Value.ToString().Equals(_archive_department_denied_user.LOGIN_USER_ID.ToString()))
                                {
                                    lstLoginUsers.SetItemChecked(i, true);
                                    break;
                                }
                            }
                        }));
                    }
                    break;
                #endregion
                case SecurityOptions.FolderStructureSecurity:
                    {
                        PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_DENIED_LOGIN_USERSLoadAllBY_FOLDERS_STRUCTURE_ID(_archive_department_folderstructure_id).ToList().ForEach(new Action<PRO_DOCS.DAL.Database.proc_ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_DENIED_LOGIN_USERSLoadAllBY_FOLDERS_STRUCTURE_IDResult>(delegate(PRO_DOCS.DAL.Database.proc_ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_DENIED_LOGIN_USERSLoadAllBY_FOLDERS_STRUCTURE_IDResult _archive_folder_denied_user)
                        {
                            for (int i = 0; i < lstLoginUsers.ItemCount; i++)
                            {
                                if (lstLoginUsers.Items[i].Value.ToString().Equals(_archive_folder_denied_user.LOGIN_USER_ID.ToString()))
                                {
                                    lstLoginUsers.SetItemChecked(i, true);
                                    break;
                                }
                            }
                        }));
                    }
                    break;
                case SecurityOptions.ModulesSecurity:
                    {
                        PRO_DOCSDatabaseDataContext.proc_APPLICATION_MODULE_DENIED_USERSLoadAllByAPPLICATION_MODULE_ID(archive_module_id).ToList().ForEach(new Action<PRO_DOCS.DAL.Database.proc_APPLICATION_MODULE_DENIED_USERSLoadAllByAPPLICATION_MODULE_IDResult>(delegate(PRO_DOCS.DAL.Database.proc_APPLICATION_MODULE_DENIED_USERSLoadAllByAPPLICATION_MODULE_IDResult _archive_modules_denied_user)
                        {
                            for (int i = 0; i < lstLoginUsers.ItemCount; i++)
                            {
                                if (lstLoginUsers.Items[i].Value.ToString().Equals(_archive_modules_denied_user.LOGIN_USER_ID.ToString()))
                                {
                                    lstLoginUsers.SetItemChecked(i, true);
                                    break;
                                }
                            }
                        }));
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
